package com.timmy.chi.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.timmy.chi.R;
import com.timmy.chi.model.beans.Food;
import com.timmy.chi.ui.activity.AdminActivity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class AdminAdapter extends BaseAdapter {


    private final AdminActivity mContext;

    private List<Food.EntryBean> mData = new ArrayList<>();

    public AdminAdapter(AdminActivity houTaiActivity) {
        mContext = houTaiActivity;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Food.EntryBean getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final Viewholder viewholder;

        if (convertView == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.admin_item, null);
            viewholder = new Viewholder();

            viewholder.tv_name = (TextView) view.findViewById(R.id.foodname);
            viewholder.tv_material = (TextView) view.findViewById(R.id.material);
            viewholder.tv_price = (TextView) view.findViewById(R.id.tv_price);
            viewholder.iv_image = (ImageView) view.findViewById(R.id.iv_image);

            view.setTag(viewholder);

        } else {
            viewholder = (Viewholder) view.getTag();
        }
        Food.EntryBean data = mData.get(position);

        String name = data.getName();
        String image = data.getImage();
        String material = data.getMaterial();
        double price = data.getPrice();

        viewholder.tv_name.setText(name);
        if (TextUtils.isEmpty(material)) {
            viewholder.tv_material.setText("材料暂无");
        } else {
            viewholder.tv_material.setText(material);
        }
        viewholder.tv_price.setText(price + "");

        Glide.with(mContext)
                .load(image)
                .into(viewholder.iv_image);
        return view;
    }

    public void setData(List<Food.EntryBean> data) {
        mData = data;

        Collections.sort(mData, new Comparator<Food.EntryBean>() {
            @Override
            public int compare(Food.EntryBean o1, Food.EntryBean o2) {
                return (o1.getSid() - o1.getSid());
            }
        });

        notifyDataSetChanged();
    }

    public void removeData(int position) {
        mData.remove(position);
        notifyDataSetChanged();
    }

    class Viewholder {
        TextView tv_name;
        TextView tv_material;
        TextView tv_price;
        ImageView iv_image;
    }
}
